home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3109 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: hilbert.dnai.com!usenet
  2. From: Victor Bazarov <vbazarov@imsisoft.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: Thu, 25 Jan 1996 17:53:42 -0800
  6. Organization: IMSI
  7. Message-ID: <31083426.197A@imsisoft.com>
  8. References: <m0tedv8-0002eqC@sice.nsk.su>
  9. NNTP-Posting-Host: 204.182.61.84
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4a (Win16; I)
  14.  
  15. Pavel A. Zemtsov wrote:
  16. >  Having declaration
  17. >  double x, p, q, r;
  18. > my compiler (cc on SCO 3.2) calculated following expression:
  19. >  x = p * q / r;
  20. > as p * (q/r); (I mean, it divided first, than multiplied). That
  21. > resulted in precision loss.
  22. > Is this legal behavior?
  23.  
  24. According to ISO/IEC 9899, it is implementation defined, I guess.
  25.  
  26. On the page 7 the behaviour of the abstract machine is described:
  27.  
  28. "In the abstract machine, all expressions are evaluated as specified
  29. by the semantics. An actual implementation need not evaluate part
  30. of an expression if it can deduce that its value is not used and
  31. that no needed side effects are produced (including any caused by
  32. calling a function or accessing a volatile object)."
  33.  
  34. From the wording it is not immediately clear if compiler is allowed
  35. to rewrite the expression. But in the example on the page 9 one can
  36. conclude that the expression a + 32760 + b + 5 can be rewritten as
  37. ((a + 32765) + b) on a machine without exceptions on integer overflow.
  38. So, in any case compiler vendor could say: 'Hey, I'm trying to help
  39. you here, avoiding overflow from multiplication.', although at the
  40. closer look, division can produce similar overflow...
  41.  
  42. I few words, put the braces the way you want the expression to be
  43. evaluated, and complain if compiler does not obey them.
  44.  
  45. > Thank you.
  46. > Pavel A.Zemtsov                                      pasha@sice.nsk.su
  47.  
  48. Victor.
  49.  
  50. -- 
  51. Signature.
  52.